import { Config } from "@/config/Config";
import { DailyDataSummaryModel } from "@/store/model/DailyDataSummaryModel";
import { OpenServerData } from "@/store/model/OpenServerData";
import { UserModel } from "@/store/model/UserModel";
import { ZoneModel } from "@/store/model/ZoneModel";
import { HttpUtil } from "@/utils/HttpUtil";
import { Logger } from "@/utils/Logger";

export class HttpClient {

    public static login(userName: string, pass: string, callback: Function = null) {
        let data: object = {
            "userName": userName,
            "pass": pass
        }
        HttpUtil.request(Config.getHttpUrl() + "/center", data, (response: any) => {
            Logger.log("HttpClient.login=", response)
            UserModel.instance.login(userName, pass);
            callback && callback()
        })
    }

    // 查询区服名下拉框接口
    public static queryServerAreaList(channelId:number, callback: Function = null) {
        let obj: object = {
            "mod": "server",
            "func": "queryServerAreaList",
            "channel_id":channelId
        }
        let url:string = Config.getHttpUrl();
        if(Config.isConnectServer){
            HttpUtil.request(url + "/center", obj, (response: any) => {
                Logger.log("HttpClient.queryServerAreaList=", response)
                let data: object = response["data"]
                if (data["code"] == 0) {
                    ZoneModel.json2SelectList(data["data"])
                    callback && callback()
                }
            })
        }else{
            callback && callback()
        }
    }

    //查询区服名下拉框接口
    public static queryServerList(channelId:number, page_num: number, page_size: number, callback: Function) {
        let obj: object = {
            "mod": "server",
            "func": "queryServerList",
            "page_num": page_num,
            "page_size": page_size,
            "channel_id":channelId
        }
        let url:string = Config.getHttpUrl();
        ZoneModel.modelList = []
        ZoneModel.totalNum = 0
        if(url && url != ""){
            HttpUtil.request(url + "/center", obj, (response: any) => {
                Logger.log("HttpClient.queryServerList=", obj, response)
                let data: object = response["data"]
                if (data["code"] == 0) {
                    ZoneModel.json2ModelList(response.data)
                    callback && callback()
                }
            })
        }else{
            callback && callback()
        }
    }

    // 修改服务器状态
    public static editServerStatus(server_id: number, server_status: number, ios_view_status: number, android_view_status: number) {
        let obj: object = {
            "mod": "server",
            "func": "editServerStatus",
            "server_id": server_id,
            "server_status": server_status,
            "ios_view_status": ios_view_status,
            "android_view_status": android_view_status,
        }
        HttpUtil.request(Config.getHttpUrl() + "/center", obj, (response: any) => {
            Logger.log("HttpClient.editServerStatus=", obj, response)
            let data: object = response["data"]
            if (data["code"] == 0) {
            }
        })
    }

    //总览数据
    public static query_data_view(channelId:number, server_id: Array<number>, device: number, start_time: number, end_time: number,
        page_num: number, page_size: number, callback: Function) {
        let obj: object = {
            "mod": "data_screen",
            "func": "query_data_view",
            "server_id": server_id,
            "device": device,
            "start_time": start_time,
            "end_time": end_time,
            "page_num": page_num,
            "page_size": page_size,
            "channel_id":channelId
        }
        DailyDataSummaryModel.modelList = []
        DailyDataSummaryModel.totalNum = 0
        let url:string = Config.getHttpUrl();
        if(url && url != ""){
            HttpUtil.request(url+ "/center", obj, (response: any) => {
                Logger.log("HttpClient.query_data_view=", obj, response)
                let data: object = response["data"]
                if (data["code"] == 0) {
                    DailyDataSummaryModel.json2ModelList(data)
                    callback && callback()
                }
            })
        }else{
            callback && callback()
        }
    }

    public static query_open_server_data_view(server_id: number, callback:Function){
        let obj: object = {
            "mod": "data_screen",
            "func": "query_open_server_data_view",
            "server_id": server_id,
        }
        OpenServerData.modelList = []
        OpenServerData.totalNum = 0
        let url:string = Config.getHttpUrl();
        if(url && url != ""){
            HttpUtil.request(url+ "/center", obj, (response: any) => {
                Logger.log("HttpClient.query_data_view=", obj, response)
                let data: object = response["data"]
                if (data["code"] == 0) {
                    OpenServerData.json2ModelList(data)
                    callback && callback()
                }
            })
        }else{
            callback && callback()
        }
    }

}